<?php
/**
 * 特殊独立奖池用户
 */
namespace App\Models\MainDB;

use Illuminate\Database\Eloquent\Model;

class IndependentPoolUser extends Model{

    protected $table = 'ln_independent_pool_user';     //数据库内完整表名

    protected $primaryKey = 'id';    //主键名

    protected $fillable = [
        'id',      //主键ID
        'user_id',         //用户id
        'add_time',
        'expired_time', //过期时间
        'total_cost', //历史累计投入
        'total_income', //历史累计收益
        'status', //1有效0无效
        'total_cost_current', //当前累积投入
        'total_income_current', //当前累积收益
        'pool_config', //奖池配置
    ];

    public $timestamps = false;

    //获取列表
    public function getIndependentPoolUserList()
    {
        $list = $this->from('ln_independent_pool_user as a')
            ->select('a.id', 'a.user_id', 'a.add_time', 'a.expired_time', 'a.status', 'a.total_income', 'a.total_cost', 'a.total_income_current', 'a.total_cost_current', 'a.pool_config', 'b.user_code', 'b.nickname', 'b.head_picture')
            ->leftJoin('ln_user_data as b', 'a.user_id', '=', 'b.user_id')
            ->where('a.status', 1)
            ->orderBy('a.id', 'desc')
            ->paginate(20);

        $type_list = app('model_independent_pool_type')->getIndependentPoolTypeList();

        $type_list = array_column($type_list, 'name', 'id');
        foreach ($list as &$v) {
            $pool_config = array_flip(explode(',', $v->pool_config));
            $pool_names = [];
            foreach ($pool_config as $k => $p) {
                $pool_names[] = $type_list[$k]??'';
            }
            $v->pool_config_list = implode(',', $pool_names);
            $v->add_time = date('Y-m-d H:i:s', $v->add_time);
            $v->expired_time = date('Y-m-d H:i:s', $v->expired_time);
        }
        return $list;
    }

    public function copyIndependentPool($user_id)
    {
        //判断用户是否已存在特殊独立奖池
        $result = app('model_independent_pool_user_detail')->where(['user_id' => $user_id])->count();
        if ($result > 0) {
            return false;
        }

        $independent_user = app('model_independent_pool_user')->where(['user_id' => $user_id])->first();
        if (!$independent_user || !$independent_user->pool_config) {
            return false;
        }
        $pool_config = explode(',', $independent_user->pool_config);
        if (!is_array($pool_config)) {
            return false;
        }
        $pool_type = $pool_config[mt_rand(0, count($pool_config) - 1)];

        $data = app('model_independent_pool')->where(['type' => $pool_type])->select('gift_id', 'number','critical_times')->get()->toArray();
        if (!$data) {
            return false;
        }
        $insertData = [];
        foreach ($data as $key => $value) {
            $insertData[] = [
                'user_id' => $user_id,
                'gift_id' => $value['gift_id'],
                'number'  => $value['number'],
                'critical_times'  => $value['critical_times'],
                'type'    => $pool_type,
            ];
        }
        if (!empty($insertData)) {
            app('model_independent_pool_user_detail')->insert($insertData);
        }
    }
}